//
//  UIView+LH.m
//  LHChanel
//
//  Created by zhouLiHuan on 2017/2/9.
//  Copyright © 2017年 zhouLiHuan. All rights reserved.
//

#import "UIView+LH.h"

@implementation UIView (LH)

+(instancetype)viewWithFrame:(CGRect)frame andBackGroundColor:(UIColor *)backColor{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = backColor;
    return view;
}

+ (instancetype)viewWithFrame:(CGRect)frame andBackGorundColor:(UIColor *)backColor haveTopLine:(BOOL)isTopLine haveBottomLine:(BOOL)isBottomLine{
    
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = [UIColor blackColor];
    if (isTopLine) {
        //顶部线
        UIView *topLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 0.5f)];
        topLineView.backgroundColor = [UIColor darkGrayColor];
        [view addSubview:topLineView];
    }
    if(isBottomLine){
        //底部线
        UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height - 0.5f, frame.size.width, 0.5f)];
        bottomLineView.backgroundColor = [UIColor darkGrayColor];
        [view addSubview:bottomLineView];
    }
    
    return view;

}

+ (instancetype)viewWithFrame:(CGRect)frame andBackGroundColor:(UIColor *)backColor borderWith:(CGFloat)borderWidth borderColor:(UIColor *)borderColor cornerRadius:(CGFloat)cornerRadius{
    
    UIView *view= [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = backColor;
    
    //线宽
    if (borderWidth > 0) {
        view.layer.borderWidth = borderWidth;
    }
    //线条颜色
    if (borderColor != nil && borderWidth > 0) {
        view.layer.borderColor = borderColor.CGColor;
    }
    //圆角
    if (cornerRadius > 0 && cornerRadius < frame.size.height && cornerRadius < frame.size.width) {
        view.layer.masksToBounds = YES;
        view.layer.cornerRadius = cornerRadius;
    }
   
    return view;
}

- (void)addCornerRadius:(CGFloat)cornerRadius andBorderColor:(UIColor *)borderColor{
    
    if (cornerRadius > 0) { //圆角
        self.layer.cornerRadius = cornerRadius;
        self.layer.masksToBounds = YES;
    }
    if (borderColor != nil) {
        self.layer.borderColor = borderColor.CGColor;
    }
}
/**
 子view(包括这个view)是否属于某个类
 */
- (UIView *)descendantOrSelfWithClass:(Class)desClass{
    if ([self isKindOfClass:desClass]) {
        return self;
    }
    
    for (UIView *childView in self.subviews) {
        UIView *view = [childView descendantOrSelfWithClass:desClass];
        if (view) {
            return view;
        }
    }

    return nil;
  
}

/**
 父控件(包括自己)是否属于某个类
 */
- (UIView *)ancestorOrSelfWithClass:(Class)anceCalss{
    
    if ([self isKindOfClass:anceCalss]) {
        return  self;
    }else if (self.superview ){
        return [self.superview ancestorOrSelfWithClass:anceCalss];
    }else{
        return nil;
    }
    
}

/**
 移除所有子控件
 */
- (void)removeAllSubviews{
    while (self.subviews.count) {
        UIView *childView = self.subviews.lastObject;
        [childView removeFromSuperview];
    }
}

/**
 相对于别的视图在屏幕上的偏移位置
 */
- (CGPoint)offsetFormView:(UIView *)otherView{
    
    CGFloat x = 0, y = 0;
    for (UIView *view = self; view && view != otherView; view = view.superview) {
        x += view.left;
        y += view.top;
    }
    
    return CGPointMake(x, y);

}


- (UIViewController *)viewController{
    
    for (UIView *next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
    
  
}

- (UIView *)findFirstResponderBeneathView:(UIView *)view{
    for (UIView *childView in view.subviews) {
        if ([childView respondsToSelector:@selector(isFirstResponder)] && [childView isFirstResponder]) {
            return childView;
        }
        
        UIView *result = [self findFirstResponderBeneathView:childView];
        if (result) {
            return result;
        }
    }
    return nil;
}

- (UIImage *)getScreenShotImage{
    
    UIGraphicsBeginImageContext(self.bounds.size);
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


- (CGFloat)left{
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)left{
    CGRect frame = self.frame;
    frame.origin.x = left;
    self.frame = frame;
}

- (CGFloat)top{
    return self.frame.origin.y;
}
- (void)setTop:(CGFloat)top{
    CGRect frame = self.frame;
    frame.origin.y = top;
    self.frame = frame;
}

- (CGFloat)right{
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight:(CGFloat)right{
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)bottom{
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom:(CGFloat)bottom{
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}

- (CGFloat)width{
    return self.frame.size.width;
}
- (void)setWidth:(CGFloat)width{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
    
}

- (CGFloat)height{
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)height{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)centerX{
    return self.center.x;
}
- (void)setCenterX:(CGFloat)centerX{
    self.center = CGPointMake(centerX, self.center.y);
}

- (CGFloat)centerY{
    return self.center.y;
}

- (void)setCenterY:(CGFloat)centerY{
    self.center = CGPointMake(self.center.x, centerY);
}

- (CGFloat)ttScreenX {
    CGFloat x = 0;
    for (UIView *view = self; view; view = view.superview) {
        x += view.left;
    }
    return x;
}

- (CGFloat)ttScreenY{
    CGFloat y = 0;
    for (UIView *view = self; view;view = view.superview) {
        y += view.top;
    }
    return y;
}

- (CGFloat)screenViewX {
    CGFloat x = 0;
    for (UIView *view = self; view; view = view.superview) {
        x += view.left;
        
        if ([view isKindOfClass:[UIScrollView class]]) {
            UIScrollView *scrollView = (UIScrollView *)view;
            x -= scrollView.contentOffset.x;
        }
    }
    return x;
}

- (CGFloat)screenViewY{
    CGFloat y = 0;
    for (UIView *view = self; view; view = view.superview) {
        y += view.top;
        
        if ([view isKindOfClass:[UIScrollView class]]) {
            UIScrollView *scrollView = (UIScrollView *)view;
            y -= scrollView.contentOffset.y;
        }
        
    }
    
    return y;
}

- (CGPoint)origin{
    return self.frame.origin;
}

- (void)setOrigin:(CGPoint)origin{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGSize)size{
    return self.frame.size;
}
- (void)setSize:(CGSize)size{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGFloat)orientationWidth{
    UIInterfaceOrientation orient = [UIApplication sharedApplication].statusBarOrientation;
    return UIInterfaceOrientationIsLandscape(orient) ? self.height : self.width;
}

- (CGFloat)orientationHeight{
    UIInterfaceOrientation orient = [UIApplication sharedApplication].statusBarOrientation;
    return UIInterfaceOrientationIsLandscape(orient) ? self.width : self.height;
}


@end






